﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;

namespace jnlamp.Web.lampinfo
{
    /// <summary>
    /// GetLamp 的摘要说明
    /// </summary>
    public class GetLamp : IHttpHandler, IRequiresSessionState
    {
        jnlamp.BLL.lampinfo bll = new jnlamp.BLL.lampinfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "querygl")
                Querygl(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["lid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int lid = 0;
                string uidstr = context.Request.Form["lid"];
                if (uidstr != "")
                   lid = int.Parse(uidstr);
                string lampid = context.Request.Form["lampid"];
                string mids = context.Request.Form["mid"];
                string lampdate = context.Request.Form["lampdate"];               
               
                string gwid = context.Request.Form["gwid"];
                decimal gjd = decimal.Parse(context.Request.Form["ljd"]);
                decimal gwd = decimal.Parse(context.Request.Form["lwd"]);
                int mid = 0;
                int.TryParse(mids, out mid);
                DateTime dt;
                if (lampdate != null && lampdate != "")
                    DateTime.TryParse(lampdate, out dt);
                else
                    dt = DateTime.Now;
                jnlamp.Model.lampinfo model = new jnlamp.Model.lampinfo();
                model.gwid = gwid;
                model.lampid = lampid;
                model.lampdate = dt;                
                model.ljd = gjd;
                model.lwd = gwd;
                model.mid = mid;
                model.lid = lid;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string lampid = context.Request.Form["lampid"];
                string mids = context.Request.Form["mid"];
                string lampdate = context.Request.Form["lampdate"];

                string gwid = context.Request.Form["gwid"];
                decimal gjd = decimal.Parse(context.Request.Form["ljd"]);
                decimal gwd = decimal.Parse(context.Request.Form["lwd"]);
                int mid = 0;
                int.TryParse(mids, out mid);
                DateTime dt;
                if (lampdate != null && lampdate != "")
                    DateTime.TryParse(lampdate, out dt);
                else
                    dt = DateTime.Now;
                jnlamp.Model.lampinfo model = new jnlamp.Model.lampinfo();
                model.gwid = gwid;
                model.lampid = lampid;
                model.lampdate = dt;
                model.ljd = gjd;
                model.lwd = gwd;
                model.mid = mid;
                if (bll.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int gid = 0;
                string uname, utruename, st, sebm = "";
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["gid"])
                {
                    int.TryParse(context.Request.Form["gid"].ToString().Trim(), out gid);
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat(" gid={0} and ", gid);
                if (uname != "")
                {
                    strWhere.AppendFormat(" lampid like '%{0}%' and ", uname);

                }
                //strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Querygl(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string bids="";
                string uname, utruename, st, sebm = "";
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.QueryString["baseids"])
                {
                    bids = context.Request.QueryString["baseids"].ToString().Trim();
                }                
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat(" bid in({0})", bids);                
                DataSet ds = bll.GetListByPagegl(strWhere.ToString(),"", 1, 10000);
                int count = bll.GetRecordCountgl(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}